﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetBay.Core.Log;
using Rio.Workflow.Runtime;
using NetBay.Core.BusinessEntities.Workflow;
using System.Configuration;

namespace Rio.Workflow.Campari.Invoicing
{
    public class Invoicing : WorkflowContext
    {

        /// <summary>
        /// Gets the type of the WKF.
        /// </summary>
        /// <value>
        /// The type of the WKF.
        /// </value>
        public override WorkflowType WkfType
        {
            get { return WorkflowType.SingleValidating; }
        }

        /// <summary>
        /// Get the name of the workflow
        /// </summary>
        public override string Name
        {
            get
            {
                return "Invoicing_Campari";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public long IndexId
        {
            get
            {
                if (ConfigurationManager.AppSettings["Invoicing_Campari_IndexId"] != null)
                {
                    return Convert.ToInt64(ConfigurationManager.AppSettings["Invoicing_Campari_IndexId"].ToString());
                }
                return 0;
            }
        }

        /// <summary>
        /// Overrides this method to get the receivers from the db by checking index value,
        /// </summary>
        /// <param name="wkfDefinition">The WKF definition.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override bool WorkflowCanStart(WorkflowDefinition wkfDefinition, CreationContext context)
        {
            //TODO: Check db mapping index receivers table
            bool wkfCanStart = false;

            //Check index value 
            string value = context.IndexValues[IndexId];
            if (ConvertStringToDouble(value) > 10.00)
            {
                wkfCanStart = true;
            }
            return wkfCanStart;
        }

        /// <summary>
        /// Overrides this method to get the receivers from the db by checking index value,
        /// then if needed starts the workflow
        /// </summary>
        /// <param name="wkfDefinition">The WKF definition.</param>
        /// <param name="context">The context.</param>
        /// <returns>
        /// True if started.
        /// </returns>
        public override WorkflowInstance StartWorkflow(WorkflowDefinition wkfDefinition, CreationContext context)
        {
            //Maybe get receivers here 
            context.ReceiverGroupId = new List<long>() { 141 }; //TODO: Get the receivers in a mapping table 

            //Forced to hierarchical
            wkfDefinition.IsHierarchical = true; 
            wkfDefinition.ReceiversType = ReceiverType.NotAvailable;

            
            return base.StartWorkflow(wkfDefinition, context);
        }


        /// <summary>
        /// Resolveds the receiver.
        /// </summary>
        /// <returns></returns>
        public override long? ResolvedNextHierarchicalReceiverId()
        {
            return 78;
        }

        /// <summary>
        /// Converts the string to double.
        /// </summary>
        /// <param name="inputString">The input string.</param>
        /// <returns></returns>
        private double ConvertStringToDouble(string inputString)
        {
            string temp = inputString.Replace('.', ',');

            double convertedString = 0.0;
            if (!Double.TryParse(temp, out convertedString))
            {
                LogManager.Instance().LogError("ConvertStringToDouble error - cannot parse the string:" + inputString);
            }
            return convertedString;
       }


    }
}
